package com.cvte.shop.service.impl;

import com.cvte.csb.core.interfaces.vo.RestResponse;
import com.cvte.csb.web.rest.ResponseFactory;
import com.cvte.shop.constants.UserConstants;
import com.cvte.shop.entity.ShopUserAll;
import com.cvte.shop.enums.RestResponceEnum;
import com.cvte.shop.enums.UserGradeEnum;
import com.cvte.shop.exception.BaseBusinessException;
import com.cvte.shop.service.ShopUserAllService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.cvte.csb.jdbc.mybatis.biz.BaseBiz;
import com.cvte.shop.mapper.ShopUserAllMapper;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 * 服务实现类
 *
 * @author 戴书博
 * @since 2020-07-27
 */
@Service
@Transactional
@Slf4j
public class ShopUserAllServiceImpl extends BaseBiz<ShopUserAllMapper, ShopUserAll> implements ShopUserAllService {

    private static final String DEMO_CACHE_NAME = "users";

    @Autowired
    private ShopUserAllMapper shopUserAllMapper;

    @Override
    public List<ShopUserAll> listUser() {
        return shopUserAllMapper.selectUserList();
    }


    @Override
    @Cacheable(value=DEMO_CACHE_NAME, key = "'user_'+#id")
    public ShopUserAll getUserById(String id) {
        log.info("userI");
        ShopUserAll shopUserAll = shopUserAllMapper.selectByPrimaryKey(id);
        return shopUserAll;
    }

    @Override
    public RestResponse verifyUser(String username, String password, HttpServletRequest request) {
        ShopUserAll shopUserAll = shopUserAllMapper.selectUserByUserName(username);
        if(shopUserAll == null){
            return ResponseFactory.buildResult(RestResponceEnum.LOGIN_ERROR_ACCOUNT.getMessage(),RestResponceEnum.LOGIN_ERROR_ACCOUNT.getCode(),null);
        }else{
            if(StringUtils.equals(password,shopUserAll.getPassword())){
                setSession(shopUserAll,request);
                return ResponseFactory.buildResult(RestResponceEnum.LOGIN_SUCCESS.getMessage());
            }else{
                return ResponseFactory.buildResult(RestResponceEnum.LOGIN_ERROR_ACCOUNT.getMessage(),RestResponceEnum.LOGIN_ERROR_ACCOUNT.getCode(),null);
            }
        }
    }


    @Override
    public void register(String username, String password) {
        if(StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){
            ShopUserAll user = new ShopUserAll();
            user.setShopUserAllPk(UUID.randomUUID().toString());
            user.setUsername(username);
            user.setNikename(username);
            user.setPassword(password);
            user.setGrade(UserGradeEnum.BRONZE.getCode());
            user.setUpdateTime(new Date());
            user.setRegisteryTime(new Date());
            user.setAssets(0);
            user.setAccountStatus(UserConstants.ACCOUNT_STATUS_NORMAL);
            shopUserAllMapper.insert(user);
        }else{
            //异常处理
            throw new BaseBusinessException(RestResponceEnum.REGISTER_ERROR.getCode(),RestResponceEnum.REGISTER_ERROR.getMessage());
        }
    }

    @Override
    public RestResponse updateUserNickname(String userId, String nickname) {
        if(StringUtils.isNotBlank(userId) &&StringUtils.isNotBlank(nickname)){
            shopUserAllMapper.updateUserNickname(nickname,userId);
            return ResponseFactory.getOkResponse(null);
        }else{
            return ResponseFactory.buildResult("字段有空值","500004",null);
        }
    }

    @Override
    public void deleteUser(String userId) {
        shopUserAllMapper.deleteByPrimaryKey(userId);
    }

    private void setSession(ShopUserAll shopUserAll,HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute(UserConstants.USER_TOKEN,shopUserAll);
    }


}